<template>
  <div class="wrapper">
    <el-row :gutter="0">
      <el-col :span="3">
        <!--suppress HtmlUnknownTarget -->
        <el-avatar :size="80" :src='headImg' title='点击更改头像'/>
      </el-col>
      <el-col :span="20">
        <div class="content">
          <div class="top">
            <span class="name">{{data['userName']}}</span>
            <el-rate class="elRate" v-model="showScore" :colors="colors" :disabled="true"/>
          </div>
          <div class="bottom">
            <span>{{data.content}}</span>
          </div>
        </div>
      </el-col>
      <!--suppress HtmlDeprecatedAttribute -->
      <el-col :span="1" align="left">
        <el-button class="fa fa-thumbs-o-up btnLike" :style="likeClass" :disabled="loading"
                   @click="handlePraise" type="text" size="mini"/>
        <span style="margin-left: 2px;" :style="likeClass">{{data['praise']}}</span>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { addPraiseById, cancelPraiseById } from '@/api/ApiSimpleReviewPraise'
import { ResultCode } from '@/constants/constants'

export default {
  name: 'GoodReviewShowItem',
  props: {
    data: {
      required: true,
      type: Object
    },
    flag: {
      required: false,
      type: [Boolean, NaN],
      default: null
    }
  },
  data () {
    return {
      headImg: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575266770070&di=50d9da00e429a929c92ae09e3f784d2c&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201902%2F13%2F20190213130110_xTPxP.thumb.700_0.jpeg',
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      score: 4,
      likeFlag: null,
      submitLoading: false
    }
  },
  computed: {
    loading () {
      return this.submitLoading
    },
    showScore () {
      return this.data && this.data['score'] ? this.data['score'] / 2 : 3
    },
    isLike () {
      if (this.likeFlag !== null) {
        return this.likeFlag
      } else if (this.data && this.data.flag !== null) {
        return this.data.flag
      } else {
        return this.flag
      }
    },
    likeClass () {
      return this.isLike ? {
        color: 'blue'
      } : {
        color: 'darkgray'
      }
    }
  },
  methods: {
    /**
     * 对当前评论点赞或取消点赞
     */
    handlePraise () {
      if (this.isLike) {
        // 取消点赞
        this.cancelPraise()
      } else {
        // 点赞
        this.praise()
      }
    },
    /**
     * 点赞
     */
    praise () {
      this.submitLoading = true
      this.likeFlag = true
      addPraiseById(this.data.id, data => {
        if (data && data.code === ResultCode.OK) {
          this.data['praise'] = data.object
        }
      }, () => {
        this.likeFlag = false
      }, () => {
        this.submitLoading = false
      })
    },
    /**
     * 取消点赞
     */
    cancelPraise () {
      this.submitLoading = true
      this.likeFlag = false
      cancelPraiseById(this.data.id, data => {
        if (data && data.code === ResultCode.OK) {
          this.data['praise'] = data.object
        }
      }, () => {
        this.likeFlag = true
      }, () => {
        this.submitLoading = false
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .wrapper{
    margin-top: 10px;

    .content{
      text-align: left;
      padding: 8px 30px;

      .top{
        margin: 8px 0;

        .name{
          display: inline-block;
          vertical-align: middle;
          font-weight: 700;
          margin-right: 25px;
          font-size: 15px;
          color: #555555;
        }

        .elRate{
          display: inline-block;
          margin-left: 16px;
        }
      }

      .bottom{
        color: #999999;
        margin-bottom: 0;
      }
    }

    .btnLike{
      margin-top: 40px;
      font-size: 20px;
    }
  }
</style>
